package jp.inaka.mironal.android.tad.content.provider;

import java.util.HashMap;

import jp.inaka.mironal.android.tad.content.ContentOpenHelper;
import jp.inaka.mironal.android.tad.content.table.CategoryTable;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class CategoryProvider extends ContentProvider{

        private static final int CATEGORY = 1;

        private static final int CATEGORY_ID = 2;

        private static UriMatcher URI_MATCHER;

        private static HashMap<String, String>  sCategoryProjectionMap;

        static {
            URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
            URI_MATCHER.addURI(CategoryTable.AUTHORITY, CategoryTable.PATH, CATEGORY );
            URI_MATCHER.addURI(CategoryTable.AUTHORITY, CategoryTable.PATH + "/#", CATEGORY_ID );

            sCategoryProjectionMap = new HashMap<String, String>();
            sCategoryProjectionMap.put(CategoryTable._ID, CategoryTable._ID);
            sCategoryProjectionMap.put(CategoryTable.NAME, CategoryTable.NAME);
        }

        ContentOpenHelper mOpenHelper;

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        int match = URI_MATCHER.match(uri);
        if( match == CATEGORY ){
            ContentValues values;
            if( initialValues == null ){
                values = new ContentValues();
            }else{
                values = new ContentValues(initialValues);
            }

            if( values.containsKey(CategoryTable.NAME) == false ){
                values.put(CategoryTable.NAME, "no name.");
            }

            long rowId;
            try{
                rowId = db.insert(ContentOpenHelper.CATEGORY_TABLE, null, values);
                if( rowId > 0 ){
                    Uri categoryUri = ContentUris.withAppendedId(CategoryTable.CONTENT_URI, rowId);
                    getContext().getContentResolver().notifyChange(categoryUri, null);
                    return categoryUri;
                }else{
                    throw new SQLException("Failed to insert row into " + uri );
                }
            }catch (SQLiteConstraintException e) {
                return null;
            }

        }else{
            throw new IllegalArgumentException();
        }

    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new ContentOpenHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(ContentOpenHelper.CATEGORY_TABLE);
        qb.setProjectionMap(sCategoryProjectionMap);

        switch (URI_MATCHER.match(uri)) {
            case CATEGORY:

                break;
            case CATEGORY_ID:
                qb.appendWhere(CategoryTable._ID
                            + " = "
                            + uri.getPathSegments().get(1));
                break;
            default:
                throw new IllegalArgumentException();
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // TODO �����������ꂽ���\�b�h�E�X�^�u
        return 0;
    }


}
